package com.mc.web;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mc.model.Article;
import com.mc.model.Path;
import com.mc.model.newsandbulletin;
import com.mc.service.ArticleService;
import com.mc.service.NewsandbulletService;
import com.mc.utils.PathUtil;
import com.mc.utils.StringUtil;

@Controller
@RequestMapping("/zsjy")
public class ZsjyAction {

	@Autowired private NewsandbulletService newsandbulletService;
	@Autowired private ArticleService articleService;
	private Path path = null;
	/**
	 * 
	 * @param modelMap
	 * @return 招生就业首页
	 */
	@RequestMapping("/zsjyindex")
	public String bkpy(ModelMap modelMap)
	{	
		//查询相关文章
		List<Article> articles = articleService.selectByMenuName("招生就业");
		//查询子菜单名称
		Set<String> subMenuNames = articleService.getSubMenuNames(articles);
		
		List<newsandbulletin> bulletins=newsandbulletService.selectzsjyb(0,5);
		StringUtil.getInstace().replaceTitle(bulletins);
		List<newsandbulletin> news = newsandbulletService.selectzsjynews();
		StringUtil.getInstace().replaceTitle(news);
		modelMap.addAttribute("news", news);
		modelMap.addAttribute("bulletins", bulletins);
		
		modelMap.addAttribute("articles",articles);
		modelMap.addAttribute("subMenuNames",subMenuNames);
		
		path = PathUtil.setPathParams(new String[]{"PackageName:Zsjy","ViewName:zsjy","ViewTitle:招生就业","smenu:zsjy","mmenu:zsjyindex"});				
		return PathUtil.returnStr(path, modelMap);
	}
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @return 研招
	 */
	@RequestMapping("/yz_{pagenow}")
	public String yznews(ModelMap modelMap,@PathVariable("pagenow")int pagenow)
	{
		
		int pnewscount = newsandbulletService.countofnb("news",32);
		//System.out.println("count---" + pnewscount);
		int pagesize = 10;
		int pagecount = getPageCount(pnewscount, pagesize);
		//System.out.println(pagecount);
		List<newsandbulletin> pnewses=newsandbulletService.selectnbbytype("news", 32,(pagenow-1)*pagesize, pagesize);
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		modelMap.addAttribute("pnewses", pnewses);
		path = PathUtil.setPathParams(new String[]{"PackageName:Zsjy","ViewName:yanzhao","ViewTitle:研招","smenu:zsjy","mmenu:yz"});
		return PathUtil.returnStr(path, modelMap);
	}
	public int getPageCount(int allcount,int pagesize)
	{
		//System.out.println("allcount---" + allcount + "--pagesize----");
		int pagecount = 0;
		if(allcount%pagesize == 0)
		{
			pagecount = allcount/pagesize;
		}else{
			pagecount = allcount/pagesize + 1;
		}
		return pagecount;
	}
	
}
